<template>
  <div>
    <div class="search-con search-con-top">
      <div>
        驾校名称：
        <Input v-model="searchData.school_name" style="width: 200px" clearable></Input>
        课程名称：
        <Input v-model="searchData.name" style="width: 200px" clearable></Input>

        <Button @click="search" type="primary" icon="ios-search" style="margin-left: 10px;">搜索</Button>
        <Button @click="reset" type="warning" icon="ios-refresh" style="margin-left: 10px;">刷新</Button>
        <Button slot="extra" @click="edit(false, false)" type="success" style="margin-left: 10px;">添加</Button>
      </div>
    </div>
    <br>
    <Table :columns="columns" :data="list" border stripe size="small">
      <template slot-scope="{ row , index}" slot="imgSlot">
        <show-img :imgUrl="row.img"></show-img>
      </template>
      <template slot-scope="{ row , index}" slot="makeSlot">
        <Button type="info" @click="edit(row, false)" size="small" style="margin-left: 1px;">编辑</Button>
        <Button type="error" @click="del(row.id)" style="margin-left: 1px;" size="small">删除</Button>
      </template>
    </Table>
    <br>

    <Paginate :total="total" @on-change="changePage" @on-page-size-change="changeSize" :current="searchData.page">
    </Paginate>


    <Modal title="课程信息" width="40" v-model="showAdd" :footer-hide="formItem.is_detail" :mask-closable="false">
      <Form ref="formValidate" :model="formItem" :label-width="80">
        <FormItem label="驾校">
          <Select v-model="formItem.school_id" filterable :multiple="formItem.id == 0">
            <Option :value="val.id" v-for="val in schoolList" :key="val.id">{{ val.name }}</Option>
          </Select>
        </FormItem>
        <FormItem label="类型">
          <Select v-model="formItem.type" filterable>
            <Option value="小车">小车</Option>
            <Option value="大车">大车</Option>
            <Option value="摩托车">摩托车</Option>
            <Option value="货车">货车</Option>
            <Option value="挂车">挂车</Option>
          </Select>
        </FormItem>
        <FormItem label="名称">
          <Input v-model="formItem.name" placeholder=""></Input>
        </FormItem>
        <FormItem label="图片">
          <MyUpload :maxNumber="1" @getImages="getImg" :defaultImags="[formItem.img]"></MyUpload>
        </FormItem>
        <FormItem label="原价">
          <Input v-model="formItem.old_price" placeholder="" type="number"></Input>
        </FormItem>
        <FormItem label="现价">
          <Input v-model="formItem.price" placeholder="" type="number"></Input>
        </FormItem>
        <FormItem label="详情">
          <RichText ref="ric" @getEditorContent="getRichTextContent" :content="formItem.detail">
          </RichText>
        </FormItem>
      </Form>
      <div slot="footer">
        <Button type="primary" size="large" long @click="editModalOk">确定</Button>
      </div>
    </Modal>
  </div>
</template>

<script>
import Paginate from "_c/paginate"
import ShowImg from '_c/show_img'
import MyUpload from "_c/my-upload"
import RichText from "_c/rich-text"
export default {
  name: "courseList",
  components: {
    Paginate, ShowImg, MyUpload, RichText
  },
  data() {
    return {
      searchData: {
        page: 1,
        limit: 10
      },
      // 列表
      columns: [
        {
          title: '驾校',
          key: 'school_name',
          align: "center"
        },
        {
          title: '类型',
          key: 'type',
          align: "center"
        },
        {
          title: '图片',
          key: 'img',
          align: "center",
          slot: "imgSlot"
        },
        {
          title: '课程名',
          key: 'name',
          align: "center"
        },
        {
          title: '原价',
          key: 'old_price',
          align: "center"
        },
        {
          title: '现价',
          key: 'price',
          align: "center"
        },
        {
          title: '创建时间',
          key: 'create_time',
          align: "center"
        },
        {
          title: '操作',
          align: "center",
          slot: 'makeSlot',
        },
      ],
      // 数据
      list: [],
      total: 0,
      formItem: {},
      showAdd: false,
      schoolList: [],
      openRichText: false,
    }
  },
  methods: {
    getImg(items) {
      items.forEach(item => {
        this.formItem.img = item.url;
      });
    },
    getRichTextContent(value) {
      this.formItem.detail = value;
    },
    // 查询管理员信息
    getList() {
      this.$axios('/admin/school/course/getList', this.searchData).then(res => {
        this.list = res.data;
        this.total = res.total;
      });
    },
    search() {
      this.searchData.page = 1;
      this.getList();
    },
    reset() {
      this.searchData = {
        page: 1,
        limit: 10,
      };
      this.getList();
    },
    changePage(page) {
      this.searchData.page = page;
      this.getList();
    },
    changeSize(size) {
      this.searchData.limit = size;
      this.getList();
    },
    customSort(column, key, order) {
      this.searchData.order_by = column.key;
      this.searchData.sort = column.order;
      this.getList();
    },
    editModalOk() {
      this.$axios('/admin/school/course/save', this.formItem).then(res => {
        this.$Message.success('添加成功');
        this.showAdd = false;
        this.openRichText = false;
        this.formItem = {};
        this.getList();
      });
    },
    edit(row, isDetail) {
      this.getSchoolList();
      if (row) {
        this.formItem = { ...row };
      } else {
        this.formItem = {
          id: 0,
          name: "",
          detail: "",
        }
      }
      this.openRichText = true;
      this.showAdd = true;
      this.$nextTick(() => {
        this.$refs.ric.setContent(this.formItem.content);
      });
    },
    del(id) {
      this.$axios('/admin/school/course/del', { id: id }).then(res => {
        this.$Message.success('删除成功');
        this.getList();
      });
    },
    getSchoolList() {
      this.$axios('/admin/school/school/getArr').then(res => {
        this.schoolList = res;
      });
    },
  },
  created() {
    this.getList();
  }
}
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>
